package com.byjh.api;


import com.byjh.app.domain.ErrorCollectSet;
import com.byjh.app.domain.Question;
import com.byjh.app.domain.QuestionOption;
import com.byjh.app.domain.QuestionRecords;
import com.byjh.app.dto.AddQuestionRecordDto;
import com.byjh.app.dto.ErrorCollectSetPostDto;
import com.byjh.app.service.*;
import com.byjh.common.annotation.Anonymous;
import com.byjh.common.core.controller.BaseController;
import com.byjh.common.core.domain.R;
import com.byjh.common.core.page.TableDataInfo;
import com.byjh.common.utils.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

@RestController
@RequestMapping("/api/question")
public class QuestionApi extends BaseController {

    @Resource
    private IChapterService chapterService;
    @Resource
    private IQuestionService questionService;
    @Resource
    private IQuestionOptionService optionService;
    @Resource
    private IErrorCollectSetService errorCollectSetService;

    @Resource
    private IQuestionRecordsService recordsService;


    /**
     * 数序刷题
     *
     * @return
     */
    @GetMapping("/sequenceQuestionList")
    public TableDataInfo sequenceQuestionList() {
        startPage();
        return getDataTable(questionService.selectQuestionList(null));
    }

    /**
     * 题目详情
     *
     * @param id
     * @return
     */
    @Anonymous
    @GetMapping("/question/{id}")
    public R<Question> questionInfo(@PathVariable("id") Long id) {
        return R.ok(questionService.selectQuestionById(id));
    }

    /**
     * 乱序刷题
     *
     * @return
     */
    @GetMapping("/shuffledQuestionList")
    public R<List<Question>> shuffledQuestionList() {
        List<Question> questionList = questionService.generateExamPaper(-1);
        // 打乱题目顺序
        Collections.shuffle(questionList);
        return R.ok(questionList);
    }

    /**
     * 易错题99题
     *
     * @return
     */
    @GetMapping("/error99QuestionList")
    public R<List<Question>> error99QuestionList() {
        return R.ok(questionService.selectError99QuestionList());
    }


    /**
     * 知识点练习
     *
     * @param subjectId
     * @return
     */
    @GetMapping("/questionList/{subjectId}")
    public R<Map<String, Object>> questionList(@PathVariable("subjectId") Long subjectId) {
        Map<String, Object> map = new HashMap<>();
        map.put("subject", chapterService.selectChapterById(subjectId));
        map.put("question", questionService.selectQuestionList(new Question().setChapterId(subjectId)));
        return R.ok(map);
    }

    /**
     * 题目选项
     *
     * @param questionId
     * @return
     */
    @Anonymous
    @GetMapping("/questionOptions/{questionId}")
    public R<List<QuestionOption>> questionOptions(@PathVariable("questionId") Long questionId) {
        return R.ok(optionService.selectQuestionOptionListByQuestionId(questionId));
    }


    /**
     * 错题 / 收藏集(根据科目ID)
     *
     * @return
     */
    @GetMapping("/errorCollectSetQuestionList/{chapterId}/{type}")
    public R<List<Question>> errorCollectSetQuestionList(@PathVariable("chapterId") Long chapterId, @PathVariable("type") Integer type) {
        return R.ok(questionService.selectErrorCollectSetQuestionList(SecurityUtils.getUserId(), chapterId, type));
    }

    /**
     * 添加到错题/收藏集
     */
    @PostMapping("/addToErrorOrCollectSet")
    public R<String> addToErrorOrCollectSet(@RequestBody @Valid ErrorCollectSetPostDto dto) {
        if (dto.getType() == 1) {
            // 更新题目错题次数
            questionService.updateErrorCount(dto.getQuestionId());
        }
        // 保存错题、收藏集
        errorCollectSetService.insertErrorSet(new ErrorCollectSet()
                .setUserId(SecurityUtils.getUserId())
                .setQuestionId(dto.getQuestionId())
                .setChapterId(dto.getChapterId())
                .setChapterName(dto.getChapterName())
                .setType(dto.getType()));
        return R.ok();
    }


    /**
     * 是否收藏
     *
     * @return
     */
    @GetMapping("/isCollect/{questionId}")
    public R<Boolean> isCollect(@PathVariable("questionId") Long questionId) {
        Long userId = SecurityUtils.getUserId();
        return R.ok(errorCollectSetService.isCollect(userId, questionId));
    }

    /**
     * 取消收藏
     *
     * @return
     */
    @DeleteMapping("/unCollect/{questionId}")
    public R<String> unCollect(@PathVariable("questionId") Long questionId) {
        Long userId = SecurityUtils.getUserId();
        errorCollectSetService.deleteErrorSetByQuestionIdAndUserId(questionId, userId);
        return R.ok();
    }

    /**
     * 添加刷题记录
     *
     * @return
     */
    @PostMapping("/addQuestionRecord")
    public R<String> addQuestionRecord(@RequestBody @Valid AddQuestionRecordDto dto) {
        Long userId = SecurityUtils.getUserId();
        recordsService.insertQuestionRecords(new QuestionRecords()
                .setUserId(userId)
                .setQuestionId(dto.getQuestionId())
                .setSubjectId(dto.getSubjectId())
                .setIsCorrect(dto.getIsCorrect()));
        return R.ok();
    }

}
